package com.apes.scm.settle.payment.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessType;
import com.apes.framework.plugin.soe.store.database.repository.BusinessTypeRepository;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 收付款执行记录
 *
 * @author wuq
 * @create 2018-12-26 17:17
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PaymentExecuteRecord.CONST_TABLE_NAME)
public class PaymentExecuteRecord extends Model {
    public static final String CONST_TABLE_NAME = "acc_Payment_Execute_Record";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "收付款执行记录", prefix = "PR")
    @Comment("单号")
    private String id; //单号

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司编码")
    private PartyRole company; //公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("部门编码")
    private Dept dept; //部门

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_id", referencedColumnName = "id", nullable = false)
    @Comment("业务伙伴编码")
    private PartyRole partner; //业务伙伴

    @Column(name = "origin_business_type", length = 32)
    @Comment("源单类型")
    private String originBusinessType;

    @Column(name = "origin", length = 30)
    @Comment("源单号")
    private String origin;

    @Column(name = "source", length = 100)
    @Comment("上游单据号码")
    private String source;

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("付款金额")
    private double amount = 0; //付款金额

    @States(name = "状态", values = {
            @State(id = "create", name = "待付款"),
            @State(id = "approve", name = "付款中"),
            @State(id = "done", name = "已付款")})
    @Column(name = "state", length = 10, nullable = false)
    private String state; //状态

    @Column(name = "create_Date")
    private Date createDate;    //业务发生日期

    @Column(name = "account_bank" , length = 32)
    @Comment("收款账户")
    private String accountBank;

    @Column(name = "bank", length = 32)
    @Comment("开户行")
    private String bank;

    @Column(name = "bank_account", length = 32)
    @Comment("开户行帐号")
    private String bankAccount;

    @Comment("支行")
    @Column(name = "subbranch", length = 200)
    private String subbranch;


    @Transient
    private String originBusinessName;

    public String getOriginBusinessName() {
        return getBusinessTypeName("originBusinessType");

    }
}
